document.addEventListener("DOMContentLoaded", function() {
    const messagesDiv = document.getElementById("messages");

    const ws = new WebSocket("ws://localhost:9090/ws/messages");

    ws.onopen = function() {
        console.log("WebSocket connection established.");
    };

    ws.onmessage = function(event) {
        const message = event.data;
        const messageElement = document.createElement("p");
        messageElement.textContent = message;
        messagesDiv.appendChild(messageElement);
    };

    ws.onclose = function() {
        console.log("WebSocket connection closed.");
    };

    ws.onerror = function(error) {
        console.error("WebSocket error:", error);
    };
});